package com.isoft.c2team3service2.controller;

import com.isoft.c2team3service2.entity.UserMessageVO;
import com.isoft.c2team3service2.mapper.UserMessageMapper;
import com.isoft.c2team3service2.mapper.UserMessageStatusMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息控制器，处理消息相关的HTTP请求
 */
@RestController
@RequestMapping("/api/messages")
public class MessageController {

    @Autowired
    private UserMessageMapper userMessageMapper;

    @Autowired
    private UserMessageStatusMapper userMessageStatusMapper;

    /**
     * 获取两个用户之间的历史消息
     */
    @GetMapping("/conversation")
    public List<UserMessageVO> getConversation(
            @RequestParam Integer userId1,
            @RequestParam Integer userId2,
            @RequestParam(defaultValue = "20") Integer limit,
            @RequestParam(defaultValue = "0") Integer offset) {
        return userMessageMapper.selectConversation(userId1, userId2, limit, offset);
    }

    /**
     * 批量标记消息为已读
     */
    @PostMapping("/read")
    public Integer markAsRead(
            @RequestParam Integer receiverId,
            @RequestBody List<Integer> messageIds) {
        return userMessageStatusMapper.batchMarkAsRead(receiverId, messageIds);
    }
}
